如何從 DataModel 動態訪問數據以獲取 Blackberry 10 中的可折疊列表 (How to access data dynamically from DataModel for collapsible list in Blackberry 10)


問題描述

如何從 DataModel 動態訪問數據以獲取 Blackberry 10 中的可折疊列表 (How to access data dynamically from DataModel for collapsible list in Blackberry 10)

I need to implement collapsible list with dynamic data in BB 10. For this i am using FilteredDataModel example provided in Github. Currently all data is hard coded in example but it is required to fill data dynamically in ListView.

Searched a lot but didn't get anything.


參考解法

方法 1:

I took a look at that example, and the data that's hardcoded is in the VegetablesDataModel::data function. That's the data you want to replace with your dynamic data. 

First you need to think of how you are going to store your data. The list has headers and each header has a sublist. One way to represent a header and the sublist of items is with a 

QPair<QString, QList<QString> >

QPair::first will be your header, and QPair::second will be the list of subitems. 

To make it easier to type you can use a typedef

typedef QPair<QString, QList<QString> > SubList;

Then to represent all the data in your ListView you need a list of the SubList struct above

QList<SubList>

Next, we want to replace how the data is returned by VegetablesDataModel. Add a new member variable to VegetablesDataModel for the above items list

QList<SubList> m_listData.

You now just need to replace the contents of the VegetablesDataModel::data and VegetablesDataModel::childCount functions. 

QVariant VegetablesDataModel::data(const QVariantList& indexPath)
{
    QString value;

    if (indexPath.size() == 1) { // Header requested
        int header = indexPath[0].toInt();
        return m_listData[header].first; // Return the header name
    }

    if (indexPath.size() == 2) { // 2nd‑level item requested
        const int header = indexPath[0].toInt();
        const int childItem = indexPath[1].toInt();
        return m_listData[header].second[childItem]; // Return the matching sublist item.

    }

    qDebug() << "Data for " << indexPath << " is " << value;

    return QVariant(value);
}

This takes care of the data, but we still need to tell the listView how many elements we have. 

int VegetablesDataModel::childCount(const QVariantList& indexPath)
{

    const int level = indexPath.size();

    if (level == 0) { // The number of top‑level items is requested
        return m_listData.length();
    }

    if (level == 1) { // The number of child items for a header is requested
        const int header = indexPath[0].toInt();
        return m_listData[header].second.length();
    }

    // The number of child items for 2nd level items is requested ‑> always 0
    return 0;
}

You should be good with everything else remaining the same. All that remains is for you to fill in m_listData with the data you want. Please mind any spelling mistakes since I don't have a chance to test my code but the logic should be there. 

(by SB24Ergin Babani)

參考文件

  1. How to access data dynamically from DataModel for collapsible list in Blackberry 10 (CC BY‑SA 3.0/4.0)

#listview #blackberry-10 #datamodel






相關問題

ListView中的序列號列 (Serial number Column in ListView)

狀態列表可繪製在預蜂窩版本上無法正常工作 (State list drawable not working properly on pre-honeycomb versions)

我可以減少列表視圖的執行時間嗎? (Can i reduce execution time of listview?)

ListView Windows 8 多個索引 (ListView Windows 8 multiple indexes)

如何通過單擊歌曲列表來播放歌曲 (How to play the song by clicking the lists of song)

Thuộc tính chi tiết trong ListView không hiển thị (Details property in ListView not displaying)

如何從 DataModel 動態訪問數據以獲取 Blackberry 10 中的可折疊列表 (How to access data dynamically from DataModel for collapsible list in Blackberry 10)

android如何將listview項目發送到另一個活動 (android How to send listview item to another activity)

實現 RecyclerView (Implementing RecyclerView)

如何刪除列表視圖底部的多餘空間? (How can I remove extraspace in listview bottom?)

ListViewItem ItemSelectionChangedEvent 觸發 4 次 [e.Selected 觸發兩次] 導致 Win32 異常未處理 (ListViewItem ItemSelectionChangedEvent Fires 4 times [e.Selected fires twice] leads to Win32 Exception Unhandled)

Kivy:如何使用 RecycleView 在 Kivy 中顯示具有可變行的列表? (Kivy: how to display a list with variable rows in Kivy with a RecycleView?)







留言討論